import Vue       from 'vue'
import VueRouter from 'vue-router'

import Login     from '../views/login.vue'
import Home      from '../views/home.vue'

import Work      from '../views/work'
import Userlist  from '../views/Muser'

import OrderList from '../views/order'

import HealthList from '../views/health'
import DetailHealth from '../views/health/detail'

import ShopList  from '../views/shop'
import ShopAdd   from '../views/shop/add'

import SetAdmin from '../views/set'

import axios from 'axios'
Vue.use(VueRouter)

  const routes = [
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta:{
      isLogin:true
    },
    
    children:[
      {
        path: '/work',
        name: 'work',
        component: Work,
       
      },
      {
        path: '/userlist',
        name: 'userlist',
        component: Userlist,
      
      },
      {
        path: '/orderlist',
        name: 'orderlist',
        component: OrderList,
       
      },
      {
        path: '/healthlist',
        name: 'healthlist',
        component:HealthList,
        
      },
      {
        path: '/healthldetail/:id/:name',
        name: 'healthldetail',
        component:DetailHealth,
        
      },
      {
        path: '/shoplist',
        name: 'shoplist',
        component: ShopList,
       
      },
      {
        path: '/shopadd',
        name: 'shopadd',
        component: ShopAdd,
       
      },
      {
        path: '/setadmin',
        name: 'setadmin',
        component: SetAdmin,

        
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path:'/',
    redirect:'/login'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局路由守卫函数

router.beforeEach((to,from,next)=>{

 if(to.matched.some(item=>item.meta.isLogin)){
      // 要做登录登录拦截

      // 接口请求 后台告知是否有登陆
      axios.get('/up/isloginin').then(data=>{
        
        if(data.data.code===1){
            next()
            return 
        }
        next('/login')
        return 
      })  
  }
  // 不做登录登录拦截
  next()
})


export default router
